package com.git.smp.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ObjectUtil {
	
	public static void copyBeans(Object source, Object target) {
		
		if(source == null || target == null) {
			return;
		}
		
		Class sourceClass = source.getClass();
		
		
		Class targetClass = target.getClass();
		
		//Field[] sourceClassFields = sourceClass.get
		
		List<Field> AllFields  = new ArrayList<Field>();
		
		AllFields.addAll(Arrays.asList(targetClass.getDeclaredFields()));
		AllFields.addAll(Arrays.asList(targetClass.getSuperclass().getDeclaredFields()));
		
		
		
			
			for (Field tarfield : AllFields) {
				/*
				 * if(!contiansName(targetClassFields, field.getName())) { continue; }
				 */
				
				//Field targetField = targetClass.getDeclaredField(field.getName());
				
				/*
				 * if(targetField != null) { if(field.getType().equals(targetField.getType())) {
				 * field.setAccessible(true); Object value = field.get(source);
				 * field.setAccessible(false); targetField.setAccessible(true);
				 * targetField.set(target, value); targetField.setAccessible(false); } }
				 */
				Object value =null;
				try {
					value = sourceClass.getMethod("get"+tarfield.getName().substring(0,1).toUpperCase()+tarfield.getName().substring(1)).invoke(source);
					targetClass.getMethod("set"+tarfield.getName().substring(0,1).toUpperCase()+tarfield.getName().substring(1),value.getClass()).invoke(target, value);
				
				} catch (Exception e) {
					
				} 
			}
	}
	
	
	protected static boolean contiansName(Field[] fields, String fieldName) {
		
		for(Field field : fields) {
			if(fieldName.equals(field.getName())) {
				return true;
			}
		}
		return false;
	}

}
